<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>用户登陆</p>
    </header>
    <!-- 表单部分 -->
    <ul class="form-box">
      <li>
        <div class="title">
          用户名：
        </div>
        <div class="content">
          <input type="text" v-model="loginRequest.username" placeholder="用户名">
        </div>
      </li>
      <li>
        <div class="title">
          密码：
        </div>
        <div class="content">
          <input type="password" v-model="loginRequest.password" placeholder="密码">
        </div>
      </li>
    </ul>
    <div class="button-login">
      <button @click="login">登陆</button>
    </div>
    <div class="button-register">
      <button @click="register">去注册</button>
    </div>
    <!-- 底部导航 -->
    <footer class="bottom-nav">
      <div class="nav-item" :class="{ active: $route.path === '/index' }" @click="$router.push('/index')">
        <i class="fa fa-home"></i>
        <span>首页</span>
      </div>
      <div class="nav-item">
        <i class="fa fa-compass"></i>
        <span>商家</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/orderList' }" @click="$router.push('/orderList')">
        <i class="far fa-file-alt"></i>
        <span>订单</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/userProfile' }" @click="$router.push('/userProfile')">
        <i class="fa fa-user"></i>
        <span>我的</span>
      </div>
    </footer>
  </div>
</template>



<script>


export default {
  name: 'Login',
  data() {
    return {
      loginRequest: {
        username: '',
        password: ''
      },
      user: {}
    }
  },
  methods: {
    login() {

      //账号和密码的简单判断 
      if (this.loginRequest.username == '') {
        alert('用户名不能为空！');
        return;
      }
      if (this.loginRequest.password == '') {
        alert('密码不能为空！');
        return;
      }

      //登录请求
      this.$axios.post('auth', this.loginRequest, {
        headers: {
          'Content-Type': 'application/json'  // 明确设置请求头为JSON格式
        }
      }).then(response => {
        // 从响应中提取token（response.data是{ id_token: string }）
        const token = response.data.id_token;
        if (token) {
          // 1. 将token存储到localStorage（或sessionStorage）中
          localStorage.setItem('token', token);
          this.$axios.get(`/user`, {
            headers: {
              'Authorization': `Bearer ${token}`
            }
          }).then(response => {
            this.user = response.data.data;
            if (this.user.authorities?.[0] === 'USER' || this.user.authorities?.[0] === 'User') {
              this.$router.push({ path: '/index' });
            }
            if (this.user.authorities?.[0] === 'BUSINESS') {
              this.$router.push({ path: '/mine' });
            }
            if (this.user.authorities?.[0] === 'ADMIN' || this.user.authorities?.[0] === 'admin') {
              this.$router.push({ path: '/Administrator' });
            }
          }).catch(error => {
            console.error(error);
          });

        } else {
          alert('登录失败：未收到有效令牌');
        }
      }).catch(error => {
        console.error('登录请求失败:', error);
        alert('用户名或密码不正确！');

      });
    },
    register() {
      this.$router.push({ path: 'register' });
    }
  }
}
</script>
<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097FF;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}

/****************** 表单部分 ******************/
.wrapper .form-box {
  width: 100%;
  margin-top: 12vw;
}

.wrapper .form-box li {
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
  display: flex;
  align-items: center;
}

.wrapper .form-box li .title {
  flex: 0 0 18vw;
  font-size: 3vw;
  font-weight: 700;
  color: #666;
}

.wrapper .form-box li .content {
  flex: 1;
}

.wrapper .form-box li .content input {
  border: none;
  outline: none;
  width: 100%;
  height: 4vw;
  font-size: 3vw;
}

.wrapper .button-login {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}

.wrapper .button-login button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  color: #fff;
  background-color: #38CA73;
  border-radius: 4px;
  border: none;
  outline: none;
}

.wrapper .button-register {
  width: 100%;
  box-sizing: border-box;
  padding: 4vw 3vw 0 3vw;
}

.wrapper .button-register button {
  width: 100%;
  height: 10vw;
  font-size: 3.8vw;
  font-weight: 700;
  /*与上面登陆按钮不同的只有颜色、背景色、边框不同*/
  color: #666;
  background-color: #EEE;
  border: solid 1px #DDD;
  border-radius: 4px;
  border: none;
  outline: none;
}

/****************** 底部菜单部分 ******************/
.wrapper .footer {
  width: 100%;
  height: 14vw;
  border-top: solid 1px #DDD;
  background-color: #fff;
  position: fixed;
  left: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.wrapper .footer li {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #999;
  user-select: none;
  cursor: pointer;
}

.wrapper .footer li p {
  font-size: 2.8vw;
}

.wrapper .footer li i {
  font-size: 5vw;
}

/* 底部导航 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 16vw;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 3vw;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 2vw;
  border-radius: 10px;
}

.nav-item.active {
  color: #0097FF;
  transform: translateY(-3px);
}

.nav-item i {
  font-size: 5.5vw;
  margin-bottom: 1vw;
  transition: all 0.3s ease;
}

.nav-item.active i {
  transform: scale(1.2);
}

.nav-item span {
  font-size: 3vw;
  font-weight: 500;
}
</style>